package edu.sctu.xiangkun.day201506.day20150623;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

import java.io.*;

/**
* Created by user04 on 2015/6/23.
*/

public class JsonUtil {
    static HttpClientBuilder builder = HttpClientBuilder.create();
    static CloseableHttpClient client = builder.build();


    /**
     * get方法
     * @throws java.io.IOException
     */
    public static String GetJson(String path) throws IOException {
        HttpGet get = new HttpGet(path);
        HttpResponse response = client.execute(get);
        HttpEntity entity =response.getEntity();

        InputStream inputStream = entity.getContent();

        String str;
        str = steamToString(inputStream);


        get.releaseConnection();
        return str;
    }
 public static JsonAppbean toJsonString(String json){

     JSONObject jsonObject = JSON.parseObject(json);
     JsonAppbean jsonAppbean = JsonAppbean.getDateObj();

    jsonAppbean.error = jsonObject.getIntValue("error");
    jsonAppbean.message = jsonObject.getString("message");
     jsonAppbean.count = jsonObject.getString("count");

//    String user = jsonObject.getString("user");
//    JSONObject jsonObject2  = JSON.parseObject(user);
//    jsonAppbean.user.nameid = jsonObject2.getString("nameid");
//    jsonAppbean.user.count = jsonObject2.getString("count");

    String list = jsonObject.getString("list");
    JSONArray jsonArray = JSON.parseArray(list);

    for(int i = 0;i<jsonArray.size();i++){
        ListAppbean listAppbean = new ListAppbean();
        listAppbean.goods_name = jsonArray.getJSONObject(i).getString("goods_name");
        listAppbean.goods_price  = jsonArray.getJSONObject(i).getString("goods_price");
        jsonAppbean.list.add(listAppbean);
    }
    return jsonAppbean;
//     ListAppbean listAppbean = new ListAppbean();
//     String str = jsonArray.getString(i);
//     JSONObject jsonObject1 = JSON.parseObject(str);
//
//     listAppbean.goods_name = jsonObject1.getString("goods_name");
//     listAppbean.goods_price = jsonObject1.getString("goods_price");
//
//     jsonAppbean.list.add(listAppbean);
// }
//    return jsonAppbean;
 }

    public static void serializableGoods(){
        try {
            ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(new File("D:/hah.txt")));
            outputStream.writeObject(JsonAppbean.getDateObj());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 流变为字符串
     * @param inputStream
     * @return
     * @throws java.io.IOException
     */
    public static String steamToString(InputStream inputStream) throws IOException {
        byte[] buffer = new  byte[1024];
        int count;
        String str = "     ";

        while((count = inputStream.read(buffer)) !=-1){                  //为什么必须循环4次才能读完
            str = str + new String(buffer,0,count,"utf-8");
        }

        return str;
    }

    public  static void printfJson(){

        try {
            JsonAppbean jsonAppbean = (JsonAppbean) new ObjectInputStream(new FileInputStream(new File("D:/hah.txt"))).readObject();
            System.out.println("error:"+jsonAppbean.error);
            System.out.println("message:"+jsonAppbean.message);
            System.out.println("count:"+jsonAppbean.count);

            for (int i = 0; i < jsonAppbean.list.size(); i++) {
                System.out.println("good"+(i+1)+":");
                System.out.println("goods_name:"+jsonAppbean.list.get(i).goods_name);
                System.out.println("goods_price:"+jsonAppbean.list.get(i).goods_price);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
